package interfaz2;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.TreeSet;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import dustconf.DustConf;

/**
 * The view of the publication option.
 * 
 * @author ole
 */
public class PublicationComposite extends DustComposite {
	// Class attributes.
	private Table pub_table;
	
	/*
	 * Private classes.
	 */
	
//	/**
//	 * Class that handles the selection of the button "Create publisher".
//	 */
//	private class CreateKeysListener extends SelectionAdapter {
//		@Override
//		public void widgetSelected(SelectionEvent event) {
//			CreatPubDialog cp_dialog;
//			Shell shell;
//			String keys_dir, keys_name, pub_name, priv_name;
//			KeyPair keypair;
//			
//			// Ask the name of the new Publisher through a Dialog.
//			shell = PublicationComposite.this.getShell();
//			cp_dialog = new CreatPubDialog(shell);
//			keys_name = (String)cp_dialog.runDialog();
//			if (keys_name != null) {
//				try {
//					// Create the public and private keys.
//					keys_dir = Util.checkDirectoryPath(DustConf.getKeysFolder());
//					keypair = KeyUtils.createNewKeyPair();
//					
//					// Save the public key to a file.
//					pub_name = KeyUtils.saveKey(keypair.getPublic(),
//												   keys_name, keys_dir);
//					
//					// Save the private key to a file.
//					priv_name = KeyUtils.saveKey(keypair.getPrivate(),
//													keys_name, keys_dir);
//					
//					// Refresh the Publishers table.
//					addKeysItem(pub_table, pub_name);
//					addKeysItem(pub_table, priv_name);
//				}
//				catch (Exception e) {
//					ExceptionBox ebox = new ExceptionBox(e, shell);
//					ebox.popError();
//				}
//			}
//		}
//	}
	
	/*
	 * Private methods.
	 */
	
	/**
	 * Method that adds a new Item to the table representing the Keys.
	 * 
	 * @param table:
	 * 		The Table to insert the TableItem.
	 * @param fname:
	 * 		The filename of the file that is a key.
	 */
	private void addKeysItem(Table table, String fname) {
		TableItem item;
		String pub_info[];
		
		pub_info = new String[1];
		item = new TableItem(table, SWT.NONE);
		pub_info[0] = fname;
		item.setText(pub_info);
	}
	
	/*
	 * Public methods.
	 */
	
	/**
	 * Class constructor.
	 * @throws FileNotFoundException 
	 */
	public PublicationComposite(Composite parent, int style) throws FileNotFoundException {
		super(parent, style);
		
		// The publication view has two zones, at the top a Composite with
		// buttons and at the bottom a Table with the keys.
		Composite buttons_comp;
//		Button addpub_button;
		TableColumn column;
		GridData grid;
//		String images_path;
		File keys_fd;
		
//		images_path = Util.checkDirectoryPath(DustConf.getImagesFolder());
		
		this.setLayout(new GridLayout());
		grid = new GridData(GridData.FILL, GridData.FILL, true, true);
		this.setLayoutData(grid);
		
		// The Composite for the buttons.
		buttons_comp = new Composite(this, SWT.NONE);
		buttons_comp.setLayout(new GridLayout());
		
//		// A button to create a new pair of keys.
//		addpub_button = new Button(buttons_comp, SWT.NONE);
//		addpub_button.setText("Create key pair");
//		addpub_button.setImage(IfaceUtils.buildImage(images_path + "keypair.gif"));
//		addpub_button.addSelectionListener(new CreateKeysListener());
		
		// The table of keys.
		pub_table = new Table(this, SWT.H_SCROLL | SWT.V_SCROLL);
		grid = new GridData(GridData.FILL, GridData.FILL, true, true);
		pub_table.setLayoutData(grid);
		pub_table.setHeaderVisible(true);
		column = new TableColumn(pub_table, SWT.NONE);
		column.setText("Filename");
		column.setWidth(200);
		
		// Fill the table with the keys.
		keys_fd = new File(DustConf.getKeysFolder());
		for (String fname: new TreeSet<String>(Arrays.asList(keys_fd.list())))
			addKeysItem(pub_table, fname);
	}
}
